package soccerBet.project.ruleEngine;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;

import org.kie.api.KieBase;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatelessKnowledgeSession;



import soccerBet.project.Calculation.AvaliationFunction;
import soccerBet.project.Calculation.Team;
import soccerBet.project.dao.ClassificacaoDAO;
import soccerBet.project.dao.JogoDAO;
import soccerBet.project.dao.PrecosDAO;
import soccerBet.project.objects.Classificacao;
import soccerBet.project.objects.Jogo;
import soccerBet.project.objects.Precos;
import soccerBet.project.services.InputValuesService;
import soccerBet.project.utils.Display;
import soccerBet.project.utils.Utils;

public class EngineStart {
	
	private KieSession ksession;
	
	public EngineStart(){
		

		this.ksession = callRuleEngine();

	}
	
	public void Start(InputValuesService ivs, Precos price) {
		try{

			this.ksession.insert(ivs.getBetRule());
			this.ksession.insert(ivs.getAvaliation().getHome());
			this.ksession.insert(ivs.getAvaliation().getAway());
			this.ksession.insert(price);
			// and fire the rules
			this.ksession.fireAllRules();
			Display.println("DR - "+ivs.getBetRule().getTipoAposta()+" - "+ivs.getBetRule().getJogo());
		}catch(Exception e){
			System.out.println("Excepcao2:"+e.getMessage());
		}
	}

	private KieSession callRuleEngine() {
		
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		
		InputStream is = 
			this.getClass().getResourceAsStream("/files/HelloWorld.drl");  
	
		
		kbuilder.add(ResourceFactory.newInputStreamResource(is),ResourceType.DRL);
		KieBase kbase = kbuilder.newKnowledgeBase();
		KieSession ksess = kbase.newKieSession();
		
		// Once the session is created, the application can interact with it
		// In this case it is setting a global as defined in the
		// org/drools/examples/helloworld/HelloWorld.drl file
		ksess.setGlobal("list", new ArrayList<Object>());

		// The application can also setup listeners
//		ksess.addEventListener(new DebugAgendaEventListener());
//		ksess.addEventListener(new DebugRuleRuntimeEventListener());

		return ksess;

		// To setup a file based audit logger, uncomment the next line
		// KieRuntimeLogger logger = ks.getLoggers().newFileLogger( ksession,
		// "./helloworld" );

		// To setup a ThreadedFileLogger, so that the audit view reflects events
		// whilst debugging,
		// uncomment the next line
		// KieRuntimeLogger logger = ks.getLoggers().newThreadedFileLogger(
		// ksession, "./helloworld", 1000 );

	}

	public void setKsession(KieSession ksession) {
		this.ksession = ksession;
	}

	public KieSession getKsession() {
		return ksession;
	}

}
